//
//  MainHomePageViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/20.
//

import UIKit

class MainHomePageViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setNavi()
        setupUI()
    }
    private func setNavi() {
        self.navigationItem.title = "智连模式"
        let leftItemButton = UIBarButtonItem(image: UIImage(named: "home_more_img"), style: .plain, target: self, action: #selector(moreItemButtonAction))
        navigationItem.leftBarButtonItem = leftItemButton
        
        let rightItemButton = UIBarButtonItem(image: UIImage(named: "home_kefu_icon"), style: .plain, target: self, action: #selector(kefuItemButtonAction))
        navigationItem.rightBarButtonItem = rightItemButton
    }
    private func setupUI() {
        view.addSubviews(topScrollLabel, downloadView, uploadView, speedView, centerButton, noteView, schemaView, tabbarView)
        
        topScrollLabel.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.right.equalToSuperview()
        }
        downloadView.snp.makeConstraints { make in
            make.top.equalTo(topScrollLabel.snp.bottom).offset(42)
            make.left.equalTo(50)
            make.height.equalTo(50)
        }
        uploadView.snp.makeConstraints { make in
            make.centerY.equalTo(downloadView.snp.centerY)
            make.left.equalTo(downloadView.snp.right).offset(50)
            make.height.equalTo(downloadView.snp.height)
            make.width.equalTo(downloadView.snp.width)
        }
        speedView.snp.makeConstraints { make in
            make.centerY.equalTo(uploadView.snp.centerY)
            make.left.equalTo(uploadView.snp.right).offset(50)
            make.height.equalTo(uploadView.snp.height)
            make.width.equalTo(uploadView.snp.width)
            make.right.equalTo(-50)
        }
        centerButton.snp.makeConstraints { make in
            make.top.equalTo(downloadView.snp.bottom).offset(25)
            make.centerX.equalToSuperview()
            make.width.equalTo(277)
            make.height.equalTo(277)
        }
        noteView.snp.makeConstraints { make in
            make.top.equalTo(centerButton.snp.bottom).offset(30)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        schemaView.snp.makeConstraints { make in
            make.top.equalTo(noteView.snp.bottom).offset(10)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
        tabbarView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(89 + ScreenSize.safeAreaInsets.bottom)
        }
    }
    @objc private func moreItemButtonAction() {
        let vc = MineViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    /// 客服
    @objc private func kefuItemButtonAction() {
        Toast.with(message: "客服").showOnWindow()
        
    }
    ///点击开始链接
    @objc private func centerButtonAction() {
        Toast.with(message: "开始链接。。。").showOnWindow()
        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
            self.centerButton.setImage(UIImage(named: "home_center_btn_connecting_img"), for: .normal)
            self.centerButton.setImage(UIImage(named: "home_center_btn_connecting_img"), for: .selected)
            self.centerButton.setImage(UIImage(named: "home_center_btn_connecting_img"), for: .highlighted)
            DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
                self.centerButton.setImage(UIImage(named: "home_center_btn_success_img"), for: .normal)
                self.centerButton.setImage(UIImage(named: "home_center_btn_success_img"), for: .selected)
                self.centerButton.setImage(UIImage(named: "home_center_btn_success_img"), for: .highlighted)
            }
        }
    }
    /// 节点
    @objc private func noteViewDidTapAction() {
        let vc = LineViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    /// 模式切换
    private func schemaClickAction() {
        Toast.with(message: "模式切换").showOnWindow()
    }
    // 自动协议
    private func autoPrivacyClickAction() {
        Toast.with(message: "自动协议").showOnWindow()
    }
    /// 热门应用
    private func hotSystemAction() {
        Toast.with(message: "热门应用").showOnWindow()
    }
    // 切换模式
    private func switchAction() {
        Toast.with(message: "模式切换").showOnWindow()
    }
    /// 购买
    private func buyAction() {
        let vc = InAppPurchaseViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var topScrollLabel: MainHomePageTopCustomView = {
        let scrollLabel = MainHomePageTopCustomView()
        return scrollLabel
    }()
    private lazy var downloadView: MainHomePageSpeedSubView = {
        let download = MainHomePageSpeedSubView()
        download.setIconImage(iconName: "download", title: "0.3KB/s")
        return download
    }()
    private lazy var uploadView: MainHomePageSpeedSubView = {
        let upload = MainHomePageSpeedSubView()
        upload.setIconImage(iconName: "upload", title: "0.3KB/s")
        return upload
    }()
    private lazy var speedView: MainHomePageSpeedSubView = {
        let speed = MainHomePageSpeedSubView()
        speed.setIconImage(iconName: "speed", title: "0.3KB/s")
        return speed
    }()
    private lazy var centerButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "home_center_btn_img"), for: .normal)
        button.setImage(UIImage(named: "home_center_btn_img"), for: .selected)
        button.setImage(UIImage(named: "home_center_btn_img"), for: .highlighted)
        button.addTarget(self, action: #selector(centerButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var noteView: MainHomePageNoteView = {
        let note = MainHomePageNoteView()
        let tap = UITapGestureRecognizer(target: self, action: #selector(noteViewDidTapAction))
        note.addGestureRecognizer(tap)
        note.set(noteIconImageView: "", titleLabel: "中国")
        return note
    }()
    private lazy var schemaView: MainHomePageSchemaView = {
        let schema = MainHomePageSchemaView()
        schema.schemaButtonActionBlock = { [weak self] in
            self?.schemaClickAction()
        }
        schema.autoPrivacyButtonActionBlock = { [weak self] in
            self?.autoPrivacyClickAction()
        }
        return schema
    }()
    private lazy var tabbarView: MainHomePageTabBarView = {
        let tabbar = MainHomePageTabBarView()
        tabbar.hotTabItemTapFunctionBlock = { [weak self] in
            self?.hotSystemAction()
        }
        tabbar.switchTabItemTapFunctionBlock = { [weak self] in
            self?.switchAction()
        }
        tabbar.buyTabItemTapFunctionBlock = { [weak self] in
            self?.buyAction()
        }
        return tabbar
    }()
}
